package com.plentitude.easytrader.ui;

import com.plentitude.easytrader.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;


/**
 * Page with application specific settings.
 * 
 * @author team@getstarted.com.ua
 */
public class NotifyActivity extends Activity implements OnCheckedChangeListener {
	//private String TAG = "NotifyActivity";
	private SharedPreferences settings;
	private CheckBox cbEnableAudio;
	private CheckBox cbEnableVibration;
	private CheckBox cbAutoOpen;
	private CheckBox cbAutoTrade;
	private CheckBox cbAutoCheck;
    
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_notification);
        
        settings = getSharedPreferences(Keys.KEY, 0);
        
        cbEnableAudio = (CheckBox) findViewById(R.id.cbEnableAudio);
        cbEnableAudio.setOnCheckedChangeListener(this);
        cbEnableVibration = (CheckBox) findViewById(R.id.cbEnableVibration);
        cbEnableVibration.setOnCheckedChangeListener(this);
        cbAutoOpen = (CheckBox) findViewById(R.id.cbAutoOpen);
        cbAutoOpen.setOnCheckedChangeListener(this);
        cbAutoTrade = (CheckBox) findViewById(R.id.cbAutoTrade);
        cbAutoTrade.setOnCheckedChangeListener(this);
        cbAutoCheck = (CheckBox) findViewById(R.id.cbAutoCheck);
        cbAutoCheck.setOnCheckedChangeListener(this);
        
        readSettings();
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
    }

   @Override
    protected void onDestroy() {
    	super.onDestroy();
    	settings = null;
    	cbEnableAudio = null;
    	cbEnableVibration = null;
    	cbAutoOpen = null;
    	cbAutoTrade = null;
    	cbAutoCheck = null;

	}
    
	@Override
	public void onCheckedChanged(CompoundButton button, boolean checked) {
		SharedPreferences.Editor editor = settings.edit();
		if(cbEnableAudio.equals(button)) {
	    	editor.putBoolean(Keys.not_cbEnableAudio, cbEnableAudio.isChecked());
		} else if(cbEnableVibration.equals(button)) {
	    	editor.putBoolean(Keys.not_cbEnableVibration, cbEnableVibration.isChecked());
		} else if(cbAutoOpen.equals(button)) {
	    	editor.putBoolean(Keys.not_cbAutoOpen, cbAutoOpen.isChecked());
		} else if(cbAutoTrade.equals(button)) {
	    	editor.putBoolean(Keys.not_cbAutoTrade, cbAutoTrade.isChecked());
		} else if(cbAutoCheck.equals(button)) {
	    	editor.putBoolean(Keys.not_cbAutoCheck, cbAutoCheck.isChecked());
		}
        editor.commit();
	}
	
	// PRIVATE METHODS
	private void readSettings() {
        cbEnableAudio.setChecked(settings.getBoolean(Keys.not_cbEnableAudio, Keys.not_cbEnableAudio_default));
        cbEnableVibration.setChecked(settings.getBoolean(Keys.not_cbEnableVibration, Keys.not_cbEnableVibration_default));
        cbAutoOpen.setChecked(settings.getBoolean(Keys.not_cbAutoOpen, Keys.not_cbAutoOpen_default));
        cbAutoTrade.setChecked(settings.getBoolean(Keys.not_cbAutoTrade, Keys.not_cbAutoTrade_default));
        cbAutoCheck.setChecked(settings.getBoolean(Keys.not_cbAutoCheck, Keys.not_cbAutoCheck_default));
	}

}
